library(haven)

# Load in Data set
impdat20_gendsci <- read_spss("./data//Gender-Science_IAT.public.2020.sav")
# Subset to USA
impdat20_gendsci <- subset(impdat20_gendsci, countryres_num == 1)

# Meta Info ---------------------------------------------------------------
impdat20_gendsci$date_mdy <- format(impdat20_gendsci$date, "%Y-%m-%d")
impdat20_gendsci$date_mdy <- as.Date(impdat20_gendsci$date_mdy)
impdat20_gendsci$week <- format(as.Date(impdat20_gendsci$date), "%W")

# Demographics ---------------------------------------------------------------
# Age
impdat20_gendsci$age[which(impdat20_gendsci$birthyear > 1900)] <- impdat20_gendsci$year[which(impdat20_gendsci$birthyear > 1900)] - impdat20_gendsci$birthyear[which(impdat20_gendsci$birthyear > 1900)]
impdat20_gendsci$age_sc <- (impdat20_gendsci$age-min(impdat20_gendsci$age, na.rm = T))/(max(impdat20_gendsci$age, na.rm = T)-min(impdat20_gendsci$age, na.rm = T))

impdat20_gendsci$age_cat4 <- NA
impdat20_gendsci$age_cat4[which(impdat20_gendsci$age >= 18 & impdat20_gendsci$age <= 29)] <- "18-29"
impdat20_gendsci$age_cat4[which(impdat20_gendsci$age >= 30 & impdat20_gendsci$age <= 44)] <- "30-44"
impdat20_gendsci$age_cat4[which(impdat20_gendsci$age >= 45 & impdat20_gendsci$age <= 64)] <- "45-64"
impdat20_gendsci$age_cat4[which(impdat20_gendsci$age >= 65)] <- "65+"
impdat20_gendsci$age_cat4 <- as.factor(impdat20_gendsci$age_cat4)

# Sex
impdat20_gendsci$sex <- NA
impdat20_gendsci$sex[which(impdat20_gendsci$birthSex == 2)] <- "f"
impdat20_gendsci$sex[which(impdat20_gendsci$birthSex == 1)] <- "m"

# Race and Ethnicity
impdat20_gendsci$race <- factor(impdat20_gendsci$raceomb_002, 
                           levels = c(1:max(impdat20_gendsci$raceomb_002, na.rm = T)),
                           labels = c("American Indian/Alaskan Native",
                                      "East Asian",
                                      "South Asian",
                                      "Native Hawaiian/Pacific Islander",
                                      "Black",
                                      "White",
                                      "Other", 
                                      "Multiracial"))

impdat20_gendsci$hisp <- NA
impdat20_gendsci$hisp[which(impdat20_gendsci$ethnicityomb == 1)] <- 1
impdat20_gendsci$hisp[which(impdat20_gendsci$ethnicityomb == 2)] <- 0 # drops unknowns

impdat20_gendsci$race5 <- NA
impdat20_gendsci$race5[which(impdat20_gendsci$race %in% c("East Asian", "South Asian", "Native Hawaiian/Pacific Islander") & impdat20_gendsci$hisp == 0)] <- "Asian/Pacific Islander"
impdat20_gendsci$race5[which(impdat20_gendsci$race == "Black" & impdat20_gendsci$hisp == 0)] <- "Black"
impdat20_gendsci$race5[which(impdat20_gendsci$race == "White" & impdat20_gendsci$hisp == 0)] <- "White"
impdat20_gendsci$race5[which(impdat20_gendsci$race %in% c("Other", "Multiracial") & impdat20_gendsci$hisp == 0)] <- "Other"
impdat20_gendsci$race5[which(impdat20_gendsci$hisp == 1)] <- "Hispanic"
impdat20_gendsci$race5 <- as.factor(impdat20_gendsci$race5)
impdat20_gendsci$race5 <- factor(impdat20_gendsci$race5, levels(impdat20_gendsci$race5)[c(1:3,5,4)])

impdat20_gendsci$race3 <- NA
impdat20_gendsci$race3[which(impdat20_gendsci$race5 == "White")] <- "White"
impdat20_gendsci$race3[which(impdat20_gendsci$race5 == "Black")] <- "Black"
impdat20_gendsci$race3[which(impdat20_gendsci$race5 != "Black" & impdat20_gendsci$race5 != "White")] <- "Other"

# Ideology
impdat20_gendsci$ideo3 <- NA
impdat20_gendsci$ideo3[which(impdat20_gendsci$politicalid_7 < 4)] <- 1
impdat20_gendsci$ideo3[which(impdat20_gendsci$politicalid_7 == 4)] <- 0
impdat20_gendsci$ideo3[which(impdat20_gendsci$politicalid_7 > 4)] <- -1
impdat20_gendsci$ideo3_lab <- factor(impdat20_gendsci$ideo3,
                                labels = c("liberal", "moderate", "conservative"))

impdat20_gendsci$ideo_lib <- ifelse(impdat20_gendsci$ideo3 == -1, 1, 0)
impdat20_gendsci$ideo_mod <- ifelse(impdat20_gendsci$ideo3 == 0, 1, 0)
impdat20_gendsci$ideo_con <- ifelse(impdat20_gendsci$ideo3 == 1, 1, 0)

impdat20_gendsci$ideo7 <- impdat20_gendsci$politicalid_7*-1 + 7
impdat20_gendsci$ideo7_sc <- impdat20_gendsci$ideo7/6

# Region
impdat20_gendsci$south <- NA
impdat20_gendsci$south <- ifelse(impdat20_gendsci$STATE %in% c("VA", "NC", "SC", "GA", "FL", "MS", "AL", "TN", "AR", "LA", "TX"),
                            1, 0)
impdat20_gendsci$south[impdat20_gendsci$STATE == ""] <- NA

impdat20_gendsci$census_region <- NA
impdat20_gendsci$census_region[which(impdat20_gendsci$STATE %in% c("DE", "MD", "DC", "WV", "KY", "OK",
                                                         "VA", "NC", "SC", "GA", "FL", "MS", "AL", 
                                                         "TN", "AR", "LA", "TX"))] <- "South"
impdat20_gendsci$census_region[which(impdat20_gendsci$STATE %in% c("CA", "OR", "WA", "MT", "WY",
                                                         "ID", "CO", "UT", "AZ", "NM",
                                                         "NV", "AK", "HI"))] <- "West"
impdat20_gendsci$census_region[which(impdat20_gendsci$STATE %in% c("ND", "SD", "MN", "WI",
                                                         "IA", "NE", "KS", "MO",
                                                         "IL", "IN", "MI", "OH"))] <- "Midwest"
impdat20_gendsci$census_region[which(impdat20_gendsci$STATE %in% c("ME", "VT", "NH", "MA", "RI",
                                                         "CT", "NY", "NJ", "PA"))] <- "Northeast"
impdat20_gendsci$census_region[impdat20_gendsci$STATE == ""] <- NA
impdat20_gendsci$census_region <- as.factor(impdat20_gendsci$census_region)

# Education
impdat20_gendsci$edu_cat <- NA
impdat20_gendsci$edu_cat[which(impdat20_gendsci$edu <= 3)] <- 1 # Less than HS
impdat20_gendsci$edu_cat[which(impdat20_gendsci$edu == 4)] <- 2 # HS 
impdat20_gendsci$edu_cat[which(impdat20_gendsci$edu == 5 | impdat20_gendsci$edu == 6)] <- 3 # Some college or associates
impdat20_gendsci$edu_cat[which(impdat20_gendsci$edu == 7)] <- 4 # College
impdat20_gendsci$edu_cat[which(impdat20_gendsci$edu >= 8)] <- 5 # Adv degree or schooling
impdat20_gendsci$col <- ifelse(impdat20_gendsci$edu_cat >= 4, 1, 0)

impdat20_gendsci$edu_cat4 <- NA
impdat20_gendsci$edu_cat4[which(impdat20_gendsci$edu_cat <= 2)] <- "HS or Less"
impdat20_gendsci$edu_cat4[which(impdat20_gendsci$edu_cat == 3)] <- "Some College"
impdat20_gendsci$edu_cat4[which(impdat20_gendsci$edu_cat == 4)] <- "College"
impdat20_gendsci$edu_cat4[which(impdat20_gendsci$edu_cat == 5)] <- "Post-Grad"
impdat20_gendsci$edu_cat4 <- factor(impdat20_gendsci$edu_cat4,
                               levels = c("HS or Less",
                                          "Some College",
                                          "College",
                                          "Post-Grad"))

# Reason for Visiting
# impdat20_gendsci$broughtwebsite
impdat20_gendsci$broughtwebsite2 <- NA
impdat20_gendsci$broughtwebsite2[which(impdat20_gendsci$broughtwebsite %in% c("Assignment for school", "Assignment for work"))] <- "Assignment school/work"
impdat20_gendsci$broughtwebsite2[which(impdat20_gendsci$broughtwebsite %in% c("Mention in a news story (any medium)"))] <- "News Mention"
impdat20_gendsci$broughtwebsite2[which(impdat20_gendsci$broughtwebsite %in% c("My Internet search for this topic or a related topic", "Mention or link at a non-news Internet site"))] <- "Internet Mention/Search"
impdat20_gendsci$broughtwebsite2[which(impdat20_gendsci$broughtwebsite %in% "Recommendation of a friend or co-worker")] <- "Peer Recommendation"
impdat20_gendsci$broughtwebsite2[which(impdat20_gendsci$broughtwebsite %in% c("Other", "null"))] <- "Other/null"

impdat20_gendsci$broughtwebsite2 <- as.factor(impdat20_gendsci$broughtwebsite2)


impdat20_gendsci$broughtwebsite4 <- NA
impdat20_gendsci$broughtwebsite4[which(impdat20_gendsci$broughtwebsite %in% c("Assignment for school", "Assignment for work"))] <- "Assignment school/work"
impdat20_gendsci$broughtwebsite4[which(impdat20_gendsci$broughtwebsite %in% c("My Internet search for this topic or a related topic", "Mention or link at a non-news Internet site", "News Mention"))] <- "News/Internet Mention/Search"
impdat20_gendsci$broughtwebsite4[which(impdat20_gendsci$broughtwebsite %in% "Recommendation of a friend or co-worker")] <- "Peer Recommendation"
impdat20_gendsci$broughtwebsite4[which(impdat20_gendsci$broughtwebsite %in% c("Other", "null"))] <- "Other/null"

impdat20_gendsci$broughtwebsite4 <- as.factor(impdat20_gendsci$broughtwebsite4)

# Interview Date Indicators -----------------------------------------------
impdat20_gendsci$post_gf <- ifelse(impdat20_gendsci$date_mdy > "2020-05-25", 1, 0)
impdat20_gendsci$post_gf_1month <- ifelse(impdat20_gendsci$date_mdy > "2020-05-25" & impdat20_gendsci$date_mdy <= "2020-06-26", 
                                     1, 0)
impdat20_gendsci$post_gf_2month <- ifelse(impdat20_gendsci$date_mdy > "2020-06-26" & impdat20_gendsci$date_mdy <= "2020-07-26", 
                                     1, 0)
impdat20_gendsci$post_gf_3month <- ifelse(impdat20_gendsci$date_mdy > "2020-07-26" & impdat20_gendsci$date_mdy <= "2020-08-26", 
                                     1, 0)
